package com.common.service.judicialpersonal.impl;

import com.common.converter.BusinessJobExperienceConverter;
import com.common.entity.personal.BusinessJobExperienceEntity;
import com.common.global.BizException;
import com.common.global.CommonEnum;
import com.common.repository.judicialpersonal.BusinessJobExperienceRepository;
import com.common.requestvo.judicialpersonal.BusinessJobExperienceReqVo;
import com.common.service.judicialpersonal.BusinessJobExperienceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * @author johnny
 * @create 2021-05-14 5:46 下午
 **/
@Service
@Slf4j
@Transactional(rollbackOn = Exception.class)
public class BusinessJobExperienceServiceImpl implements BusinessJobExperienceService {

    @Autowired
    private BusinessJobExperienceRepository businessJobExperienceRepository;

    @Override
    public void add(BusinessJobExperienceReqVo businessJobExperienceReqVo) {
        businessJobExperienceReqVo.setId(null);
        BusinessJobExperienceEntity businessJobExperienceEntity = BusinessJobExperienceConverter.INSTANCE.vo2domain(businessJobExperienceReqVo);
        businessJobExperienceRepository.save(businessJobExperienceEntity);
    }

    @Override
    public void edit(BusinessJobExperienceReqVo businessJobExperienceReqVo) {
        if (businessJobExperienceReqVo.getId() != null && businessJobExperienceReqVo.getId() != 0L) {
            if (businessJobExperienceRepository.existsById(businessJobExperienceReqVo.getId())) {
                BusinessJobExperienceEntity businessJobExperienceEntity = BusinessJobExperienceConverter.INSTANCE.vo2domain(businessJobExperienceReqVo);
                businessJobExperienceRepository.save(businessJobExperienceEntity);
            } else {
                throw new BizException(CommonEnum.ID_NOT_FIND_ENTITY);
            }
        } else {
            throw new BizException(CommonEnum.ID_NOT_NULL);
        }
    }

    @Override
    public void delete(Long id) {
        if (businessJobExperienceRepository.existsById(id)) {
            businessJobExperienceRepository.deleteById(id);
        } else {
            throw new BizException(CommonEnum.ID_NOT_FIND_ENTITY);
        }
    }
}